package light.head.dao.oa;

import java.util.ArrayList;

import java.util.Date;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.ApplyInfo;
import light.head.model.ApplyItem;
import light.head.model.AuditStatus;

import org.springframework.stereotype.Service;
/***
 * 物品申请
 * @author yy
 *
 */
@Service
public class ApplyInfoDAO extends BaseDAO<ApplyInfo, Integer>{
	
	private final static String sql_findApplyItem = " FROM ApplyItem ad WHERE ad.acode = ?";
	private final static String sql_findByMcAuditStatus = " FROM AuditStatus a WHERE a.mc = ? ";
	private final static String sql_findApplyInfo = " FROM ApplyInfo a WHERE a.cu = ?";
	
	/***
	 * 根据跟机构及子机构编号查询申请单
	 * @param ocs 跟机构及子机构编号
	 * @param map
	 * @param pb
	 * @return
	 */
	public RESULT findByMap(List<Integer> ocs, Map<String, String> map , PageBean pb){
		if (null == ocs || ocs.size() == 0) return null;
		
		StringBuffer hql = new StringBuffer("FROM ApplyInfo a WHERE a.oc IN (");
		List<Object> list = new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String title = map.get(Parm.TITLE);
			String status = map.get(Parm.STATUS);
			if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND a.deleted = ?");
			}
			if (isNotNull(status)) {
				list.add(Byte.parseByte(status));
				hql.append(" AND a.status = ?");
			}
			if (isNotNull(time1)) {
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND a.at >= ?");
			}
			if(isNotNull(time2)){
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append("AND a.at <= ?");
			}
			if(isNotNull(title)) {
				list.add("%" + title + "%");
				hql.append("AND a.title LIKE ?");
			}
			
		}	 
		hql.append(" AND (1 = (SELECT s.status FROM AuditStatus s WHERE s.business = 1 and s.mc = a.code and a.sort = 2 and s.pc = (SELECT max(pc) FROM AuditStatus where business = 1 and mc = s.mc))OR a.sort = 1)");
		hql.append(" ORDER BY a.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	/***
	 * 根据当前用户查询申请单
	 * @param au 当前用户编号
	 * @param map
	 * @param pb
	 * @return
	 */
	public RESULT findByMap_2(Integer au, Map<String, String> map , PageBean pb){
		if (null == au) return null;
		
		StringBuffer hql = new StringBuffer("FROM ApplyInfo a WHERE a.au = ?");
		List<Object> list = new ArrayList<Object>();
		
		list.add(au);
		if (map != null) {
			String deleted = map.get(Parm.DELETED);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String title = map.get(Parm.TITLE);
			String status = map.get(Parm.STATUS);
			if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND a.deleted = ?");
			}
			if (isNotNull(status)) {
				list.add(Byte.parseByte(status));
				hql.append(" AND a.status = ?");
			}
			if (isNotNull(time1)) {
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND a.at >= ?");
			}
			if (isNotNull(time2)) {
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append("AND a.at <= ?");
			}
			if (isNotNull(title)) {
				list.add("%" + title + "%");
				hql.append("AND a.title LIKE ?");
			}
		}	 
		hql.append(" ORDER BY a.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	/**
	 * 审核申请单(查询出待审核的物品申请单：1.上一级审核已经通过的， 2.本机构员工所提交的)
	 * @param ocs	当前机构的跟机构下的所有子机构集合
	 * @param rc	角色编号
	 * @param map	查询条件
	 * @param pb	分页信息
	 */
	public RESULT findByMap_1(List<Integer> ocs, Integer rc, Map<String, String> map , PageBean pb){
		if (null == ocs || null == rc) return null;
		
		List<Object> parm = new ArrayList<Object>();	
		List<Object> countParm = new ArrayList<Object>();
		
		StringBuffer temp = new StringBuffer();
		temp.append(" FROM ApplyInfo a");
		temp.append(" JOIN UserInfo u ON u.code = a.au");
		temp.append(" JOIN Organ o ON o.code = a.oc");
		temp.append(" JOIN AuditStatus s on s.mc = a.code and s.beforeStatus = 1 WHERE a.deleted = 0 AND s.business = 1 AND s.rc = ? and a.oc IN (");
		
		parm.add(rc);
		countParm.add(rc);
		for (Integer ig : ocs) {
			temp.append("?,");
			parm.add(ig);
			countParm.add(ig);
		}
		
		temp.deleteCharAt(temp.length() - 1).append(")");
		
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String title = map.get(Parm.TITLE);
			
			if (isNotNull(deleted)) {
				parm.add(Boolean.parseBoolean(deleted));
				countParm.add(Boolean.parseBoolean(deleted));
				temp.append(" AND a.deleted = ?");
			}
			if (isNotNull(time1)) {
				parm.add(DateUtils.getDateByStr(time1, null));
				countParm.add(DateUtils.getDateByStr(time1, null));
				temp.append(" AND a.at >= ?");
			}
			if(isNotNull(time2)){
				parm.add(DateUtils.getDateByStr(time2, null));
				countParm.add(DateUtils.getDateByStr(time2, null));
				temp.append(" AND a.at <= ?");
			}
			if(isNotNull(title)) {
				parm.add("%" + title + "%");
				countParm.add("%" + title + "%");
				temp.append(" AND a.title LIKE ?");
			}
		}	 
		
		StringBuffer sql = new StringBuffer("SELECT a.code, a.title, a.au, a.at, a.oc, a.deleted, u.realName, o.name");
		StringBuffer countSql = new StringBuffer("SELECT COUNT(*)");
		
		sql.append(temp).append(" ORDER BY a.code DESC");
		countSql.append(temp);
		
		if (RESULT.SUCCESS == queryToPageBean(pb, sql.toString(), parm, countSql.toString(), countParm))
			return packageResult(pb);
		else
			return RESULT.ERROR;
		}
	/***
	 * 封装
	 * @param pb
	 * @return
	 */
	private RESULT packageResult(PageBean pb) {
		
		ApplyInfo info = null;
		List<ApplyInfo> list = new ArrayList<ApplyInfo>();
		
		//  a.code, a.title, a.au, a.at, a.oc, a.deleted, u.realName, o.name
		for (Object o : pb.getResultList()) {
			
			info = new ApplyInfo();
			Object[] obj = (Object[]) o;
			
			Integer code = (null == obj[0] || Sys.NONE.equals(obj[0].toString())) ? null : (Integer) obj[0];
			String title = (null == obj[1] || Sys.NONE.equals(obj[1].toString())) ? null : (String) obj[1];
			Integer au = (null == obj[2] || Sys.NONE.equals(obj[2].toString())) ? null : (Integer) obj[2];
			Date at = (null == obj[3] || Sys.NONE.equals(obj[3].toString())) ? null : (Date) obj[3];
			Integer oc = (null == obj[4] || Sys.NONE.equals(obj[4].toString())) ? null : (Integer) obj[4];
			Boolean deleted = (null == obj[5] || Sys.NONE.equals(obj[5].toString())) ? null : (Boolean) obj[5];
			String an = (null == obj[6] || Sys.NONE.equals(obj[6].toString())) ? null : (String) obj[6];
			String ocn = (null == obj[7] || Sys.NONE.equals(obj[7].toString())) ? null : (String) obj[7];
			
			info.setCode(code);
			info.setTitle(title);
			info.setAu(au);
			info.setAt(at);
			info.setOc(oc);
			info.setDeleted(deleted);
			info.setAn(an);
			info.setOcn(ocn);
			
			list.add(info);
		}
		pb.setResultList(list);
		return RESULT.SUCCESS;
	}
	
  /***
   * 根据申请单编号查询申请明细
   * @param code
   * @return
   */
	public List<ApplyItem> findApplyItem(Integer code) {
		if (null == code)return null;
		List<ApplyItem> list = find(sql_findApplyItem, code);
		return list;
	}
	
	/***
	 * 
	 * @param mc
	 * @param rc
	 * @return
	 */
	public List<AuditStatus> findByMcAuditStatus(Integer mc) {
		if (null == mc )return null;
		return find(sql_findByMcAuditStatus, null, null, mc);
	}
	
	
	/***
	 * 查询当前登录人的所有申请信息
	 * @param cu
	 * @return
	 */
	public List<ApplyInfo> findApplyInfo(Integer cu) {
		if (null == cu || null == cu)return null;
		return find(sql_findApplyInfo, null, null, cu);
	}
	
}
